package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.Station;
import com.hzhc.mapper.StationMapper;
import com.hzhc.service.StationService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.StationVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 站点信息 服务实现类
 * </p>
 *
 * @author 1
 * @since 2024-05-30
 */
@Service
@RequiredArgsConstructor
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {

    @Override
    public List<StationVo> getStationList(Page<StationVo> page, StationVo vo) {
        IPage<StationVo> iPage = baseMapper.getStationList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addStation(Station station) {
        if (save(station)){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateStation(Station station) {
        if (updateById(station)){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> delStation(Long id) {
        Station station = getById(id);
        station.setDisable(1);
        if (updateById(station)){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public List<Station> getStation(Station station) {
        return baseMapper.getStation(station);
    }

    @Override
    public Boolean checkStation(Station station) {
        LambdaQueryWrapper<Station> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Station::getDisable,0)
                .eq(Station::getStation,station.getStation())
                .eq(Station::getType,station.getType());
        if (station.getId() !=null){
            wrapper.ne(Station::getId,station.getId());
        }
        int count = count(wrapper);
        return count<=0;

    }


}
